package com.bjsxt.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.domain.Market;
import com.bjsxt.domain.TradeArea;
import com.bjsxt.dto.R;
import com.bjsxt.service.MarketService;
import com.bjsxt.service.TradeAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/markets")
@Api(tags = "交易市场")
public class MarketController {
    @Autowired
    private MarketService marketService;

    @GetMapping
    @ApiOperation(value = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "交易区域名称"),
            @ApiImplicitParam(name = "status", value = "交易区域状态"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数"),
    })
    @PreAuthorize("hasAuthority('trade_market_query')")
    public R<Page<Market>> findByPage(
            Long tradeAreaId, Byte status,
            @ApiIgnore Page<Market> page

    ) {
        Page<Market> marketPage = marketService.findByPage(page, tradeAreaId, status);
        return R.ok(marketPage);
    }

    @PostMapping("/setStatus")
    @ApiOperation(value = "启用/禁用交易市场")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "market", value = "market的json数据"),
    })
    @PreAuthorize("hasAuthority('trade_market_update')")
    public R setStatus(@RequestBody Market market) {
        boolean isOk = marketService.updateById(market);
        if (isOk)
            return R.ok();
        return R.ok("状态设置失败");
    }

    @PostMapping
    @ApiOperation(value = "新增市场")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "market", value = "market的json数据"),
    })
    @PreAuthorize("hasAuthority('trade_market_create')")
    public R save(@RequestBody Market market) {
        boolean isOk = marketService.save(market);
        if (isOk)
            return R.ok();
        return R.ok("新增失败");
    }

    @PatchMapping
    @ApiOperation(value = "修改市场")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "market", value = "market的json数据"),
    })
    @PreAuthorize("hasAuthority('trade_market_update')")
    public R update(@RequestBody Market market) {
        boolean isOk = marketService.updateById(market);
        if (isOk)
            return R.ok();
        return R.ok("修改失败");
    }

    @GetMapping("/all")
    @ApiOperation(value = "查询所有的交易市场")
    @PreAuthorize("hasAuthority('trade_market_query')")
    public R<List<Market>> listMarks() {
        return R.ok(marketService.list());
    }
}
